package io.github.bigbird0101.datatransfer.task.handler;

import cn.hutool.json.JSONUtil;
import integration.data.model.dto.batch.SrcParamDTO;
import integration.data.model.dto.batch.TgtParamDTO;
import integration.data.model.entity.DataIntegration;
import integration.data.model.entity.TaskDefinition;
import integration.data.utils.CodeGenerateUtils;
import integration.modle.enums.ErrorCodeEnum;
import integration.modle.exception.IntegrationValidException;

import java.util.ArrayList;
import java.util.List;

/**
 * 多表任务参数处理
 */
public class MigrationTaskDefHandler extends IntegrationTaskDefHandler {


    @Override
    public List<TaskDefinition> doTask(DataIntegration param) {

        List<String> mappingList = JSONUtil.toList(param.getJobMapping(), String.class);
        List<SrcParamDTO> srcList = JSONUtil.toList(param.getJobSrcJson(), SrcParamDTO.class);
        List<TgtParamDTO> tgtList = JSONUtil.toList(param.getJobTgtJson(), TgtParamDTO.class);

        List<TaskDefinition> taskDefList = new ArrayList<>();

        for (int i = 0; i < srcList.size(); i++) {

            SrcParamDTO subSrc = srcList.get(i);
            String srcJson = JSONUtil.toJsonStr(subSrc);
            String mappingJson = JSONUtil.toJsonStr(mappingList.get(i));
            TgtParamDTO subTgt = tgtList.get(i);
            String tgtJson = JSONUtil.toJsonStr(subTgt);

            String s = subSrc.getTables().toString();
            String t = subTgt.getTableName();
            // 每个子任务修改名称
            // s在list.toString已经带了'[]'，不需要再加'[]'，t需要额外添加'[]'
            String subName = String.format("%s:%s_to_%s", param.getJobName(), s, t);
            TaskDefinition taskDefinition = buildTaskDefinitionJson(param, srcJson, tgtJson, mappingJson);
            taskDefList.add(taskDefinition);

        }

        return taskDefList;
    }


    protected TaskDefinition buildTaskDefinitionJson(DataIntegration param, String sourceJson, String targetJson, String mapping) {

        // 集成相关任务类型：STREAM_INTEGRATION, INTERGRATION, STREAM_UNSTRUCTURED, UNSTRUCTURED
        String taskType = getTaskType(param.getIntegrationType());
//        param.setIntegrationType(taskType);

        String taskParams = buildTaskParam(param, sourceJson, targetJson, mapping);

        long code = 0;
        try {
            code = CodeGenerateUtils.getInstance().genCode();
        } catch (CodeGenerateUtils.CodeGenerateException e) {
            throw new IntegrationValidException(ErrorCodeEnum.DATA_INTEGRATION_TASK_CODE_ERROR);
        }

        TaskDefinition taskDefinition = TaskDefinition.builder()
                .code(code)
                .tenantInstanceId(param.getTenantInstanceId())
                .integrationId(param.getId())
                .applicationId(param.getApplicationId())
                .name(param.getJobName())
                .jobId(param.getJobId())
                .description(param.getJobDesc())
                .taskType(taskType)
                .taskParams(taskParams)
                .timeout(param.getTimeout())
                .scheduleParams(param.getJobCron())
                .build();

        return taskDefinition;
    }

}
